'use strict';

var createError = require('./createError');

/**
 * 根据响应的`status`判断是调用`reslove`还是`reject`
 *
 * @param {Function} resolve Promise的`resolve`
 * @param {Function} reject Promise的`reject`
 * @param {object} response 响应
 */
module.exports = function settle(resolve, reject, response) {
  var validateStatus = response.config.validateStatus;
  if (!response.status || !validateStatus || validateStatus(response.status)) {
    resolve(response);
  } else {
    reject(
      createError(
        'Request failed with status code ' + response.status,
        response.config,
        null,
        response.request,
        response
      )
    );
  }
};
